package com.force.jianghuling.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.force.jianghuling.R;
import com.force.jianghuling.base.BaseActivity;
import com.force.jianghuling.httputils.DataListener;
import com.force.jianghuling.httputils.HttpUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;


/**
 * 注册界面
 * 使用leanCloud 注册登录
 * Created by zzj on 2016/11/30.
 */
@ContentView(R.layout.act_register)
public class RegisterActivity extends BaseActivity implements View.OnClickListener,DataListener {
    @ViewInject(R.id.input_name)
    private EditText usernameEt;
    private String username;
    @ViewInject(R.id.input_password)
    private EditText passwordEt;
    private String password;
    @ViewInject(R.id.input_phone)
    private EditText phoneEt;
    private String phone;
    @ViewInject(R.id.btn_signup)
    private AppCompatButton registerBt;
    @ViewInject(R.id.link_login)
    private TextView loginTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        initBackTitle("注册界面");
        registerBt.setOnClickListener(this);
        loginTv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_signup:
                username = usernameEt.getText().toString();
                password = passwordEt.getText().toString();
                phone = phoneEt.getText().toString();
                showLog("获取的用户名：" + username);
                if (username.equals("")) {
                    showToast("用户名不能为空！");
                    return;
                }
                if (password.equals("")) {
                    showToast("密码不能为空！");
                    return;
                }
                if (phone.equals("")) {
                    showToast("手机号不能为空！");
                    return;
                }

                /**
                 * curl -X POST \
                 -H "X-LC-Id: i5bb1lWlF4KzN0wc0X5WMmfP-gzGzoHsz" \
                 -H "X-LC-Key: XC2MRatuIkSRTjKnu7kG722u" \
                 -H "Content-Type: application/json" \
                 -d '{"username":"hjiang","password":"f32@ds*@&dsa","phone":"18612340000"}' \
                 https://api.leancloud.cn/1.1/users
                 */
                showProgressDialog();
                HttpUtils.register(context,this,HttpUtils.PUBLIC_URL+"/users",username,password,phone);
                break;

            case R.id.link_login:
                //跳转登录页面
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                finish();
                break;

        }
    }

    @Override
    public void onSuccess(String json, String type) {
        dismissProgressDialog();
        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
        RegisterActivity.this.finish();
    }

    @Override
    public void onFail(Exception e, String type) {
        dismissProgressDialog();
        Toast.makeText(RegisterActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
